<?php

App::uses('CrudController', 'Controller');
App::uses('WorkstationConfig', 'Model');

class WorkstationsConfigsController extends CrudController
{
    public $_model = 'WorkstationConfig';

    public function admin($workstationId, $lineId, $areaId, $moeId) {
        $this->request->allowMethod('get');
        $this->loadModel('Area');
        $this->loadModel('Moe');
        $this->loadModel('Line');
        $this->loadModel('Workstation');
        $this->loadModel('WorkstationConfig');
        $appBosch = new stdClass();
        $appBosch->workstationId = $workstationId;
        $appBosch->lineId = $lineId;
        $appBosch->areaId = $areaId;
        $appBosch->moeId = $moeId;
        $appBosch->workstationsConfigs = $this->WorkstationConfig->getEnabledByWorkstation($workstationId);
        $description = $this->getDescription($workstationId, $lineId, $areaId, $moeId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Temas'));
        $this->set('description', $description);
    }

    private function getDescription($workstationId, $lineId, $areaId, $moeId) {
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        $this->Area->id = $areaId;
        $area = $this->Area->read();
        $this->Line->id = $lineId;
        $line = $this->Line->read();
        $this->Workstation->id = $workstationId;
        $workstation = $this->Workstation->read();
        $parts = array(
            $moe['Moe']['name'],
            $area['Area']['name'],
            $line['Line']['name'],
            $workstation['Workstation']['description'],
        );
        return implode(' - ', $parts);
    }

    /**
     * AJAX
     * Abilities/admin
     * @param string $workstationId UUID
     * @param string $userId UUID
     * @return string JSON
     */
    public function getByWorkstation($workstationId, $userId, $certificationId = null) {
        $record = new stdClass();
        if ($this->request->allowMethod('get') === true && $this->request->is('ajax') === true) {
            if ($certificationId === null) {
                $record->certification = $this->getNewCertification($workstationId, $userId);
            } else {
                $this->loadModel('Certification');
                $record->certification = $this->Certification->getCertificationWithDetails($certificationId);
                $this->loadModel('CertificationDetail');
                $record->details = $this->Certification->getDetails($certificationId);
                if(count($record->details)==0){
                    $record->details = $this->getNewDetails($workstationId, $userId);
                }
            }
        }
        $this->set(array('records' => $record, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    /**
     * Esta función obtiene información para una certificación que no existe
     * @param string $workstationId UUID
     * @param string $userId UUID
     * @return array
     */
    private function getNewCertification($workstationId, $userId) {
        $this->loadModel('Workstation');
        $this->Workstation->id = $workstationId;
        $workstation = $this->Workstation->read();
        $data = array();
        if (isset($workstation['Workstation']) === true) {
            $w = $workstation['Workstation'];
            $data['wId'] = $w['id'];
            $data['wDays'] = $w['days'];
            $data['wMaxDays'] = $w['max_days'];
        }
        $this->loadModel('User');
        $this->User->id = $userId;
        $user = $this->User->read();
        if (isset($user['User']) === true) {
            $data['uId'] = $user['User']['id'];
        }
        return $data;
    }

    protected function getRecords($param)
    {
        $m = $this->_model;
        $records = $this->$m->getEnabledByWorkstation($param);
        return $records;
    }

    protected function c($model)
    {
        return array(
            'workstation_id' => trim($model->workstation_id),
            'name' => mb_strtoupper(trim($model->name)),
            'minimum_qualification' => (double)$model->minimum_qualification,
            'orden' => (int)$model->orden,
            'questions' => (int)$model->questions,
            'time' => (int)$model->time * 60,
        );
    }

    protected function resultCreate($model)
    {
        $model['time'] /= 60;
        return $model;
    }

    protected function resultUpdate($id, $result)
    {
        $result['time'] /= 60;
        return $result;
    }

    protected function u($model)
    {
        return array(
            'name' => mb_strtoupper(trim($model->name)),
            'minimum_qualification' => (double)$model->minimum_qualification,
            'orden' => (int)$model->orden,
            'questions' => (int)$model->questions,
            'time' => (int)$model->time * 60,
        );
    }

    

}
